<?php

class SJB_PluginAbstract
{
	function pluginSettings()
	{
		return array();
	}
	
	public static function init() {}
	
	/**
	 * @static
	 * @param array $modules config array of modules
	 * @param SJB_Autoloader $autoloader
	 * @return bool
	 */
	protected static function registerModules($modules = array(), SJB_Autoloader $autoloader)
	{
		if (self::isCorrectModules($modules)) {
			$moduleManager = SJB_System::getModuleManager();
			foreach ($modules as $moduleName => $moduleParams) {
				foreach ($moduleParams['functions'] as $name => $params) {
					$moduleManager->modules[$moduleName]['functions'][$name] = $params;
				}
			}
			return true;
		}
		return false;
	}
	
	/**
	 * @static
	 * @param array $modules
	 * @return bool
	 */
	protected static function isCorrectModules($modules = array())
	{
		/*
		// Sample of correct config array format
		$pluginModules = array(
			'miscellaneous' => array(
				'functions' => array(
					'api' => array(
						'display_name' => 'api',
						'script' => 'api.php',
						'raw_output' => true,
						'type' => 'user',
						'access_type' => array('user'),
					),
				),
			),
		);
		*/
		
		if (empty($modules) || !is_array($modules)) {
			return false;
		}
		
		foreach ($modules as $moduleName => $moduleParams) {
			// check module
			if (!is_string($moduleName) || !is_array($moduleParams)) {
				return false;
			}
			// check functions key
			if (!array_key_exists('functions', $moduleParams) || !is_array($moduleParams['functions'])) {
				return false;
			}
			// check functions
			foreach ($moduleParams['functions'] as $name => $params) {
				if (!is_string($name) || !is_array($params)) {
					return false;
				}
				// check function params
				if (!isset($params['script']) || !isset($params['type'])) {
					return false;
				}
			}
		}
		
		return true;
	}
	
}